// pages/coupon/index.js
const service = require("../../service/coupon")
const rules = require("../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 1,
    showtip: false,
    pageNum: 1, // 当前请求的page
    pageSize: 10, // 请求数据的size
    noData: false,
    couponList: [],
    nomoredata:false
  },
  getList(callback) {
    service.getcouponList({
        "data": {
          "cutomerId": wx.getStorageSync('userInfo').id,
          "status": this.data.currentTab
        },
        "queryRequest": {
          "pageSize": this.data.pageSize,
          "pageNum": this.data.pageNum,
        }

      },
      (res) => {
        //console.log(res);
        for (let i = 0; i < res.data.rows.length; i++) {
          const element = res.data.rows[i];
          element.startTime = rules.formateDate(element.startTime)
          element.endTime = rules.formateDate(element.endTime)
        }
        callback(res);

      }
    );
  },
  /**
   * 生命周期函数--监听页面加载
   */
  switchtag(e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    })
    this.onLoad()
  },
  switchtip() {
    this.setData({
      showtip: !this.data.showtip
    })

  },
  onLoad: function (options) {
    wx.showLoading({
      title: "加载中...",
    });
    this.getList(res => {
      console.log(res)
      wx.hideLoading()
      if (res.code == "00000") {
        if (res.data.rows.length==0) {
          this.setData({
            couponList: res.data.rows,
            noData:true,
            nomoredata:false
          })
        } else {
          this.setData({
            couponList: res.data.rows,
            noData:false,
            nomoredata:false
          })
        }
        
        
      }

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;

    this.getList(function (res) {
      console.log(res);
      if (res.code == "00000") {
        if (res.data.rows.length==0) {
          self.setData({
            couponList: res.data.rows,
            noData:true,
            nomoredata:false
          })
        } else {
          self.setData({
            couponList: res.data.rows,
            noData:false,
            nomoredata:false
          })
        }      
        
      }
      
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.noData) return;
    wx.stopPullDownRefresh();
    const prolist = this.data.couponList;
    let page = this.data.pageNum;
    this.setData({
      // 每次下拉 page+1
      pageNum: ++page,
    });
    const self = this;

    this.getList(function (res) {
      if (res.data.rows.length == 0) {
        self.setData({
          noData: false,
          nomoredata:true
        });
      } else {
        res.data.rows.forEach((item) => {
          
          prolist.push(item);
        });
        self.setData({
          couponList: prolist,
        });
      }
      // 隐藏加载状态
      // wx.hideNavigationBarLoading()
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})